#!/bin/sh
echo "postinst"

DBDIR=/home/www/servers/meterserv/cgi-bin

echo "*********************************************************************"
echo "* Setting up Meterserv database                                     *"
echo "*********************************************************************"
if [ -f $DBDIR/meters.db ]; then
  echo "I see you already have a meters database in "$DBDIR". I will leave it alone."
  echo "Meterserv should continue to work on this database."
  echo "If the database structure had changed, I would have offered to sort it"
  echo "out for you.".
else
  echo "No database found.  Installing an example one with a small amount of"
  echo "data for testing purposes.".
  echo "When you are ready to start for real, you can use the web interface"
  echo "to delete the example data.".
  cp $DBDIR/meters.db.example $DBDIR/meters.db
fi
echo ""
echo "*********************************************************************"
echo "* Starting metermon daemon..                                        *"
echo "*********************************************************************"
ln -s ../init.d/metermon /etc/rc3.d/S99metermon
ln -s ../init.d/metermon /etc/rc4.d/S99metermon
ln -s ../init.d/metermon /etc/rc5.d/S99metermon
/etc/init.d/metermon start
echo ""
echo "*********************************************************************"
echo "* Configuring system upgrade script                                 *"
echo "*********************************************************************"
# Note SUID root is necessary because the cgi script that calls this runs
#   as a normal user, but it needs root privileges.  Similarly for the daemon
#   start-stop process metermonctrl
chmod +s   /home/www/servers/meterserv/cgi-bin/sysupdate.cgi
chmod +s   /usr/bin/metermonctrl

echo ""
echo "*********************************************************************"
echo "* Configuring Web Interface                                         *"
echo "*********************************************************************"

if [ -f /etc/lighttpd.conf ]; then
   echo "I am going to change your /etc/lighttpd.conf web server configuration file."
   echo "This will not cause any trouble, unless you use the web server for something"
   echo "else."
   echo "I will back up /etc/lighttpd.conf to /etc/lighttpd.conf.meterserv.orig"
   echo " so that you can un-pick what I have done if necessary....."
   echo ""
   cp -f /etc/lighttpd.conf /etc/lighttpd.conf.meterserv.orig
   cp -f /etc/lighttpd.conf.meterserv /etc/lighttpd.conf   
else
   cp -f /etc/lighttpd.conf.meterserv /etc/lighttpd.conf
fi

if [ -d /home/www/logs ]; then
   echo "/home/www/logs already exists - leaving well alone!!"
else
   echo "Creating /home/www/logs for the web server log files"
   # Note - assumes /home/www already exists - created by meterserv package.
   mkdir /home/www/logs
fi
chown www-data.www-data /home/www/logs
chown -R www-data.www-data /home/www/servers/meterserv

echo "Re-Starting web server"
# Note - we do not use the 'restart' command because the default /etc/init.d/lighttpd
# script has a bug in it.
/etc/init.d/lighttpd stop
/etc/init.d/lighttpd start

echo ""                                                                         
echo "*********************************************************************"    
echo "* DONE! - Point your web browser at this machine to see if it works *"    
echo "*********************************************************************"    
 
return 0

